<?php
namespace V1;
class MyProduct extends  \ResourceClass{
    public $isActive = true;
    public $remark = '我的实物奖品';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require uid ';
        }
        $checkRes = $this->autoCheck($req['uid']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        try {
            $userRank_orm = new \Orm\UserRank\UserRankModel();
            $where = 'a.`is_del` = 0 AND a.`user_id` = '.$req['uid'];
            if(isset($req['type'])){
                $where .=" AND a.`type` = {$req['type']}";
            }
            if(isset($req['week_type'])){
                $where .=" AND a.`week_type` = {$req['week_type']}";
            }
            $userRank_list = $userRank_orm->getRankStorageList($where, 0, 5);//最多中五个实物奖品
            $result = $userRank_list['list'];
        }catch (\Exception $e) {
            $result = array();
        }
    }

    /**
     * @param array $req
     * @param array $post
     * @param $result
     * 更新实物奖品是否已经阅读
     */
    public function put(array $req, array $post, &$result){
        if (!isset($req['uid'])) {
            return 'require uid ';
        }
        $checkRes = $this->autoCheck($req['uid']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        if(!$post['payload']){
            return 'data error';
        }

        $data = base64_decode($post['payload']);
        @parse_str($data,$data);
        try {
            $userRankBiz = new \Biz\UserRank();
            $userRank['id'] = $data['id'];
            $userRank['read'] = $data['read'];//更新为已阅
            $userRankBiz->update($userRank);
            $time = date('Y-m-d',time());//当总排行已经出来后
            if($time>='2018-07-16'){
                $userRankBiz->updateByUid($req['uid']);
            }
            $result = array();
        }catch (\Exception $e) {
            $result = array();
        }
    }
}